Communication with HyperTerminal [ QT and WINApi ]
Posted
by javaAmator
on Stack Overflow
See other posts from Stack Overflow
or by javaAmator
Published on 2010-05-08T18:55:22Z
Indexed on
2010/05/08
18:58 UTC
Read the original article
Hit count: 493
Hi! I write program to communicate with modem (it useing Hayes commands) and this is working. GUI is programmed with QT, but communication with COM port is write with winapi library. I have problem when I want to send with my program message from one computer to another, i can't send Polish chars (they are repleaced by '?'), how can I fix it ? Does anyone have idea ?? And I have one more problem, I can't send message from my program to Microsoft HyperTerminal, HyperTerminal receive something, but not that what I send. Thx for any help :)
Important pieces of code: Connect with port:
portHandle = CreateFile (portName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
GetCommState (portHandle, &dcb);
switch(ui->comboBox->currentIndex())
{
case 0 : dcb.BaudRate=CBR_110; break;
case 1 : dcb.BaudRate=CBR_300; break;
case 2 : dcb.BaudRate=CBR_600; break;
case 3 : dcb.BaudRate=CBR_1200; break;
case 4 : dcb.BaudRate=CBR_2400; break;
case 5 : dcb.BaudRate=CBR_4800; break;
case 6 : dcb.BaudRate=CBR_9600; break;
case 7 : dcb.BaudRate=CBR_14400; break;
case 8 : dcb.BaudRate=CBR_19200; break;
case 9 : dcb.BaudRate=CBR_38400; break;
case 10 : dcb.BaudRate=CBR_56000; break;
case 11 : dcb.BaudRate=CBR_57600; break;
case 12 : dcb.BaudRate=CBR_115200; break;
case 13 : dcb.BaudRate=CBR_128000; break;
case 14 : dcb.BaudRate=CBR_256000; break;
}
dcb.fBinary = TRUE;
dcb.fParity = TRUE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fAbortOnError = FALSE;
//dcb.ByteSize = dataBits;
dcb.DCBlength = sizeof (DCB);
switch(ui->comboBox_3->currentIndex())
{
case 1 : dcb.Parity = EVENPARITY; break;
case 3 : dcb.Parity = MARKPARITY; break;
case 2 : dcb.Parity = ODDPARITY; break;
case 4 : dcb.Parity = SPACEPARITY; break;
case 0 : dcb.Parity = NOPARITY; break;
}
switch (ui->comboBox_4->currentIndex())
{
case 0 : dcb.StopBits = ONESTOPBIT; break;
case 1 : dcb.StopBits = ONE5STOPBITS;break;
case 2 : dcb.StopBits = TWOSTOPBITS; break;
}
switch (ui->comboBox_2->currentIndex())
{
case 0 : dcb.ByteSize = 5; break;
case 1 : dcb.ByteSize = 6;break;
case 2 : dcb.ByteSize= 7; break;
case 3 : dcb.ByteSize = 8; break;
}
SetCommState (portHandle, &dcb);
GetCommTimeouts (portHandle, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts (portHandle, &CommTimeouts);
Send MSG:
void MainWindow::Send(char c)
{
do
{WriteFile(portHandle, &c, 1, &cbWritten, NULL);
}
while (!(cbWritten));
}
void MainWindow::on_pushButton_clicked()
{
QString str = ui->lineEdit->text();
std::string str2;
ui->lineEdit->clear();
str2 = str.toStdString();
for(int i=0; i < str2.size();i++)
{
Send(str2[i]);
//qDebug()<< str2[i];
}
Send(char(13));
}
Receive MSG:
void ReaderThread::run()
{
char c;
while(1)
{
c = Receive();
if(c==13)
{
emit insertPlainText("\n");
}
else
{
emit insertPlainText(QString(c));
}
}
}
char ReaderThread::Receive()
{
char c;
do{
ReadFile(portHandle, &c, 1, &cbRead, NULL);
}
while (!(cbRead));
return c;
}
© Stack Overflow or respective owner